package com.chuangsi.project.admin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chuangsi.common.enums.DrivingExperienceEnum;
import com.chuangsi.common.enums.FirstCarEnum;
import com.chuangsi.common.enums.SaleStatusEnum;
import com.chuangsi.common.exception.ServiceException;
import com.chuangsi.common.utils.ExcelUtil;
import com.chuangsi.common.utils.SecurityUtils;
import com.chuangsi.framework.aspectj.lang.annotation.DataScope;
import com.chuangsi.project.admin.domain.Defeat;
import com.chuangsi.project.admin.dto.admin.defeat.ReqDefeatPageDto;
import com.chuangsi.project.admin.dto.admin.defeat.ReqFollowUpRecordsAddDto;
import com.chuangsi.project.admin.dto.admin.defeat.ResDefeatPageDto;
import com.chuangsi.project.admin.dto.admin.defeat.app.ReqDefeadAddDto;
import com.chuangsi.project.admin.mapper.DefeatMapper;
import com.chuangsi.project.admin.service.DefeatService;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 战败记录 服务实现类
 * </p>
 *
 * @author 
 * @since 2024-05-10
 */
@Service
public class DefeatServiceImpl extends ServiceImpl<DefeatMapper, Defeat> implements DefeatService {


    @Override
    public void addDefeat(ReqDefeadAddDto req) {
        Defeat defeat = new Defeat();
        defeat.setUserId(SecurityUtils.getUserId());
        defeat.setName(req.getName());
        defeat.setPhone(req.getPhone());
        defeat.setBrand(req.getBrand());
        defeat.setModel(req.getModel());
        defeat.setDeptId(req.getDeptId());
        defeat.setSaleStatus(req.getSaleStatus());
        defeat.setDrivingExperience(req.getDrivingExperience());
        defeat.setFirstCar(req.getFirstCar());
        defeat.setReason(req.getReason());
        defeat.setCreateTime(LocalDateTime.now());
        defeat.insert();
    }

    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<ResDefeatPageDto> pageQuery(ReqDefeatPageDto dto) {
        List<ResDefeatPageDto> dataList = baseMapper.pageQuery(dto);
        for (ResDefeatPageDto resDefeatPageDto : dataList) {
            resDefeatPageDto.setSaleStatus(SaleStatusEnum.getName(resDefeatPageDto.getSaleStatus()));
            resDefeatPageDto.setDrivingExperience(DrivingExperienceEnum.getName(resDefeatPageDto.getDrivingExperience()));
            resDefeatPageDto.setFirstCar(FirstCarEnum.getName(resDefeatPageDto.getFirstCar()));
        }
        return dataList;
    }

    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public void export(ReqDefeatPageDto dto, HttpServletResponse response) {
        List<ResDefeatPageDto> data = baseMapper.export(dto).stream().peek(x->{
            x.setSaleStatus(SaleStatusEnum.getName(x.getSaleStatus()));
            x.setDrivingExperience(DrivingExperienceEnum.getName(x.getDrivingExperience()));
            x.setFirstCar(FirstCarEnum.getName(x.getFirstCar()));
        }).collect(Collectors.toList());
        ExcelUtil.export(data,ResDefeatPageDto.class,"战败记录"+ LocalDate.now(),response);

    }

    @Override
    public void addFollowUpRecords(ReqFollowUpRecordsAddDto req) {
        Defeat defeat = getById(req.getDefeatId());
        if(defeat == null){
            throw new ServiceException("未找到战败记录");
        }
        defeat.setFollowUpRecords(req.getFollowUpRecords());
        defeat.setUpdateTime(LocalDateTime.now());
        defeat.updateById();
    }

    @Override
    public List<ResDefeatPageDto> appPageQuery(ReqDefeatPageDto dto) {
        dto.setUserId(SecurityUtils.getUserId());
        return pageQuery(dto);
    }

    @Override
    public void delDefeat(Integer defeatId) {
        removeById(defeatId);
    }

}
